/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.project.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.project.dao.ProjectApplyDao;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectApply;
import com.jeeplus.modules.project.entity.vo.ProjectStatisticsVo;

/**
 * 项目申报表单Service
 * @author jacky
 * @version 2017-12-10
 */
@Transactional 
@Service
public class ProjectApplyService extends CrudService<ProjectApplyDao, ProjectApply> {
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	

	public ProjectApply get(String id) {
		return super.get(id);
	}
	
	public List<ProjectApply> findList(ProjectApply projectApply) {
		return super.findList(projectApply);
	}
	
	public Page<ProjectApply> findPage(Page<ProjectApply> page, ProjectApply projectApply) {
		return super.findPage(page, projectApply);
	}
	
	public void save(ProjectApply projectApply) {
		   // 申请发起
			if (StringUtils.isBlank(projectApply.getId())){
				projectApply.preInsert();
				//填表日期，设置成当前的日期
				projectApply.setFillingFormDate(new Date());
				
				dao.insert(projectApply);
				// 启动流程
				actTaskService.startProcess(ActUtils.PD_PROJECT_APPLY[0], ActUtils.PD_PROJECT_APPLY[1], projectApply.getId(), projectApply.getBasePtInfo().getCourseName());
				//更新状态 ，已经发起申请了
				BaseProjectInfo baseInfo = new BaseProjectInfo();
				baseInfo.setId(projectApply.getBasePtInfo().getId());
				baseInfo.setStatus(2);
				try {
					baseProjectInfoService.updateStatus(baseInfo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			// 重新编辑申请		
			else{
				projectApply.preUpdate();
				dao.update(projectApply);

				projectApply.getAct().setComment(("yes".equals(projectApply.getAct().getFlag())?"[重申] ":"[销毁] ")+projectApply.getAct().getComment());
				
				// 完成流程任务
				Map<String, Object> vars = Maps.newHashMap();
				vars.put("pass", "yes".equals(projectApply.getAct().getFlag())? "1" : "0");
				actTaskService.complete(projectApply.getAct().getTaskId(), projectApply.getAct().getProcInsId(), projectApply.getAct().getComment(), projectApply.getBasePtInfo().getCourseName(), vars);
			}
	}
	
	public void approveSave(ProjectApply apply) {
		// 设置意见
		apply.getAct().setComment(("yes".equals(apply.getAct().getFlag())?"[同意] ":"[驳回] ")+apply.getAct().getComment());
		
		apply.preUpdate();
		
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = apply.getAct().getTaskDefKey();

		// 审核环节
		if ("dept-review".equals(taskDefKey)){
			apply.setDeptApproveAdvice(apply.getAct().getComment());
		}
		else if ("dept-leader-review".equals(taskDefKey)){
			apply.setDeptLeaderAdvice(apply.getAct().getComment());
		}
		else if ("academic-committee-review".equals(taskDefKey)){
			apply.setAcademyAcademiaAdvice(apply.getAct().getComment());
		}
		else if ("finance-dept-review".equals(taskDefKey)){
			apply.setFinanceAdvice(apply.getAct().getComment());
		}
		else if ("sci-research-magt-review".equals(taskDefKey)){
			apply.setSciresearchDeptAdvice(apply.getAct().getComment());
		}
		else if ("big-boss-review".equals(taskDefKey)){
			apply.setBossLeaderAdvice(apply.getAct().getComment());
		}
		// 未知环节，直接返回
		else{
			return;
		}
		//更新数据
		dao.update(apply);
		
		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		if ("academic-committee-review".equals(taskDefKey)){
			vars.put("aca_pass", "yes".equals(apply.getAct().getFlag())? "1" : "0");
		}
		if ("finance-dept-review".equals(taskDefKey)){
			vars.put("fin_pass", "yes".equals(apply.getAct().getFlag())? "1" : "0");
		}
		//最后一步如果同意，则更新项目信息状态
		if ("big-boss-review".equals(taskDefKey)){
			if("yes".equals(apply.getAct().getFlag())){
				BaseProjectInfo baseInfo = apply.getBasePtInfo();
				baseInfo.setStatus(3);
				try {
					baseProjectInfoService.updateStatus(baseInfo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		vars.put("pass", "yes".equals(apply.getAct().getFlag())? "1" : "0");
		actTaskService.complete(apply.getAct().getTaskId(), apply.getAct().getProcInsId(), apply.getAct().getComment(), vars);
	}
	
	public void delete(ProjectApply projectApply) {
		super.delete(projectApply);
	}
	/**
	 * 历年统计项目数据
	 * @param map
	 * @return
	 */
	public List<ProjectStatisticsVo> projectApplyStatistics(Map<String, Object> map){
		List<ProjectStatisticsVo> list = dao.projectApplyStatistics(map);
		return list;
	}
	
	
}